
---------------------------------------------------------------------------------------
-- RTBB_GPIO Init Script
---------------------------------------------------------------------------------------

-- This file must be loaded after the init_board.lua ;

function GPIOSet( pin, isOutputMode )

	local nResult = RTBB_GPIOSingleSetIODirection( g_hBridgeBoard, pin, isOutputMode );

	if( resultMsg( "RTBB_GPIOSingleSetIODirection", nResult ) ~= true )
	then
		return false
	end

	if( isOutputMode == true ) then
		strMode = "Output"
	else
		strMode = "Input"
	end

	dbgprintf( "Set GPIO Pin%d : %s\n", pin, strMode );
	return true
end

function GPIOR( pin )

	local nResult, bValue = RTBB_GPIOSingleRead( g_hBridgeBoard, pin );

	if( resultMsg( "RTBB_GPIOSingleRead", nResult ) ~= true )
	then
		return 0, false
	end

	value = 0
	if( bValue == true) then value = 1 end

	dbgprintf( "Read GPIO Pin%d : %d\n", pin, value );
	return value, true;
end

function GPIOW( pin, output )

	bOutput = false
	if( output ~= 0 ) then bOutput = true end

	local nResult = RTBB_GPIOSingleWrite( g_hBridgeBoard, pin, bOutput )

	if( resultMsg( "RTBB_GPIOSingleWrite", nResult ) ~= true )
	then
		return false
	end

	dbgprintf( "Write GPIO Pin%d : %d\n", pin, output )
	return true
end


function GPIOPortSet( nPort, mask, value )

	local nResult = RTBB_GPIOSetIODirection( g_hBridgeBoard, nPort, mask, value )

	if( resultMsg( "RTBB_GPIOSetIODirection", nResult ) ~= true )
	then
		return false
	end

	dbgprintf( "Set GPIO Port%d : 0x%x\n", nPort, value )
	return true
end

function GPIOPortGet( nPort)

	local nResult,value = RTBB_GPIOGetIODirection( g_hBridgeBoard, nPort)

	if( resultMsg( "RTBB_GPIOGetIODirection", nResult ) ~= true )
	then
		return false
	end

	dbgprintf( "Get GPIO Port%d : 0x%x\n", nPort, value )
	return true
end

function GPIOPortWrite(nPort, mask, value)

	local nResult = RTBB_GPIOWrite( g_hBridgeBoard, nPort, mask, value )

	if( resultMsg( "RTBB_GPIOWrite", nResult ) ~= true )
	then
		return false
	end

	dbgprintf( "Write GPIO Port%d : 0x%x\n", nPort, value )
	return true
end

function GPIOPortRead(nPort)

	local nResult, value = RTBB_GPIORead( g_hBridgeBoard, nPort)

	if( resultMsg( "RTBB_GPIORead", nResult ) ~= true )
	then
		return false
	end

	dbgprintf( "Read GPIO Port%d : 0x%x\n", nPort, value )
	return true
end




